package com.shujia.core

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

import scala.util.Random

object Demo19PI {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()

    /**
     * 提交到yarn上运行，这个参数依旧不用设置
     */
//    conf.setMaster("local")
    conf.setAppName("yarn submit")

    val context = new SparkContext(conf)

    //设置生成点的个数 10000
    val list: Range.Inclusive = 0 to 1000000000
    //将scala的序列集合变成rdd
    val rangeRDD: RDD[Int] = context.parallelize(list)

    //随机生成正方形内的点
    val dianRDD: RDD[(Double, Double)] = rangeRDD.map((i: Int) => {
      val x: Double = Random.nextDouble() * 2 - 1
      val y: Double = Random.nextDouble() * 2 - 1
      (x, y)
    })
//    println(dianRDD.count())

    //取出圆中点的个数
    val yuanZuoRDD: RDD[(Double, Double)] = dianRDD.filter {
      case (x: Double, y: Double) =>
        x * x + y * y < 1
    }
//    println(yuanZuoRDD.count())

    //计算PI
    println("="*100)
    println(s"PI的值为：${(yuanZuoRDD.count().toDouble / dianRDD.count()) * 4}")
    println("="*100)

    /**
     * spark-submit --class com.shujia.core.Demo19PI --master yarn --deploy-mode client  spark-1.0.jar
     */

  }

}
